package com.allwees.bs.core.model.order.wf.order;

import com.baomidou.mybatisplus.core.enums.IEnum;
import com.allwees.core.wf.FsmState;
import com.allwees.core.wf.Transition;
import lombok.Setter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public enum EOrderState implements FsmState<EOrderState, EOrderAction>, IEnum<String> {

    START("Start", "Start", "Start", false, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Collections.singletonList(
                    new Transition<>(EOrderAction.USER_CREATE, CREATED)
            );
        }
    },

    CREATED("Pendding Payment", "Unpaid","Unpaid",true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    new Transition<>(EOrderAction.USER_CANCEL, CANCELED),
                    new Transition<>(EOrderAction.SYSTEM_CANCEL_BY_UNPAID, CANCELED),
                    new Transition<>(EOrderAction.USER_PAYED, WAIT_SHIP),
                    new Transition<>(EOrderAction.USER_COD, WAIT_SHIP)
            );
        }
    },
    WAIT_SHIP("Waiting For Shipment", "Waiting For Shipment", "Waiting For Shipment", true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    new Transition<>(EOrderAction.MERCHANT_SHIPPED, SHIPPING),
                    new Transition<>(EOrderAction.MERCHANT_ALL_SHIPPED, SHIPPED),
                    new Transition<>(EOrderAction.SYSTEM_CLOSE_BY_SHIPPING_OVER_TIME, CANCELED),
                    new Transition<>(EOrderAction.SYSTEM_CLOSE_BY_ALL_REFUND, REFUNDED),
                    new Transition<>(EOrderAction.USER_ALL_CANCELED, CANCELED)
            );
        }
    },
    SHIPPING("Processing", "Shipping", "Shipping", true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    new Transition<>(EOrderAction.MERCHANT_SHIPPED, SHIPPING),
                    new Transition<>(EOrderAction.MERCHANT_ALL_SHIPPED, SHIPPED),
                    new Transition<>(EOrderAction.USER_ALL_CANCELED, CANCELED)
            );
        }
    },

    SHIPPED("Shipped", "Shipped", "Shipped", true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    // new Transition<>(EOrderAction.USER_CONFIRM_RECEIVED, RECEIVED),
                    // new Transition<>(EOrderAction.USER_ALL_RECEIVED, RECEIVED),
                    new Transition<>(EOrderAction.LOGISTICS_DELIVERED, DELIVERED),
                    new Transition<>(EOrderAction.USER_ALL_CANCELED, CANCELED)
            );
        }
    },

    /**
     * 待商家配送
     */
    WAIT_DELIVER("Waiting For Deliver", "Waiting For Deliver", "Waiting For Deliver", true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Collections.emptyList();
        }
    },

    /**
     * 商家已配送
     */
    DELIVERED("Delivered", "Delivered", "Delivered", true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    new Transition<>(EOrderAction.USER_CONFIRM_RECEIVED, RECEIVED),
                    new Transition<>(EOrderAction.USER_ALL_RECEIVED, RECEIVED)
            );
        }
    },

    /**
     * 用户已确认收货(待评价)
     */
    RECEIVED("Received", "Received","Received",true, false) {
        @Override
        public List<Transition<EOrderState, EOrderAction>> getTransitions() {
            return Arrays.asList(
                    new Transition<>(EOrderAction.USER_ALL_REVIEWED, COMPLETED),
                    new Transition<>(EOrderAction.SYSTEM_CLOSE_BY_ALL_REFUND, REFUNDED)
            );
        }
    },

    REFUNDED("Refunded", "Refunded","Refunded",false, true),

    CANCELED("Canceled", "Canceled", "Canceled", false, true),

    CLOSED("Closed", "Closed", "Closed", false, true),

    COMPLETED("Completed","Completed","Completed", false, true);

    /**
     * 状态描述
     */
    private final String desc;

    private final String merchantDesc;
    @Setter
    private String customerDesc;

    private final boolean started;

    private final boolean ended;

    EOrderState(String desc, String merchantDesc, String customerDesc, boolean started, boolean ended) {
        this.desc = desc;
        this.merchantDesc = merchantDesc;
        this.customerDesc = customerDesc;
        this.started = started;
        this.ended = ended;
    }

    @Override
    public String getValue() {
        return name();
    }

    public String desc() {
        return desc;
    }

    @Override
    public boolean started() {
        return started;
    }

    @Override
    public boolean ended() {
        return ended;
    }

    public String merchantDesc() {
        return merchantDesc;
    }

    public String customerDesc() {
        return customerDesc;
    }
}
